//
//  DLRefreshFooter.m
//  DGGMobileFrameworksDemo
//
//  Created by 伍明鹏 on 2019/12/23.
//  Copyright © 2019 伍明鹏. All rights reserved.
//

#import "DLRefreshFooter.h"
#import "DLRefreshAnimationView.h"
#import <Masonry/Masonry.h>

@interface DLRefreshFooter()
@property (nonatomic, strong) DLRefreshAnimationView *refreshAnimationView;

@property (nonatomic, strong) UIImageView *footerIcon;

@end

@implementation DLRefreshFooter

- (void)prepare {
    [super prepare];
    [self setTitle:@"" forState:MJRefreshStateIdle];
    [self setTitle:@"" forState:MJRefreshStateRefreshing];
    [self setTitle:@"没有更多" forState:MJRefreshStateNoMoreData];
    self.stateLabel.hidden = YES;
    [self.stateLabel setFont:[UIFont systemFontOfSize:12]];
    self.stateLabel.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.45];
}

- (void)placeSubviews {
    [super placeSubviews];
    // 设置子控件位置
    self.loadingView.hidden = YES;
    if (self.stateLabel.hidden) {
        self.refreshAnimationView.hidden = NO;
    }else{
        [self.refreshAnimationView removeFromSuperview];
        self.refreshAnimationView = nil;
    }
    
    [self.refreshAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(35);
    }];
}

- (void)setState:(MJRefreshState)state {

    MJRefreshCheckState;
   
    switch (state) {
        case MJRefreshStateIdle:
            NSLog(@"MJRefreshStateIdle");
//            self.footerIcon.hidden = YES;
            break;
        case MJRefreshStatePulling:
            NSLog(@"MJRefreshStatePulling");
//            self.footerIcon.hidden = YES;
            break;
        case MJRefreshStateRefreshing:
            if (self.refreshAnimationView && self.refreshAnimationView.hidden == YES) {
                self.refreshAnimationView.hidden = NO;
            }
            if (!self.refreshAnimationView) {
                [self show];
            }
            [self.refreshAnimationView refreshStateRefreshingAnimation];
//            self.footerIcon.hidden = YES;
            NSLog(@"MJRefreshStateRefreshing");
            break;
        case MJRefreshStateWillRefresh:
            NSLog(@"MJRefreshStateWillRefresh");
//            self.footerIcon.hidden = YES;
            break;
        case MJRefreshStateNoMoreData:
            self.stateLabel.hidden = NO;
            self.stateLabel.text = self.noDataStr;
            self.footerIcon.hidden = self.noDataStr.length > 0;
            NSLog(@"MJRefreshStateNoMoreData");
            break;
        default:
            break;
    }
}


- (void)endRefreshing {
    [super endRefreshing];
    self.refreshAnimationView.hidden = YES;
    self.stateLabel.hidden = YES;
}

- (void)setNoDataStr:(NSString *)noDataStr{
    _noDataStr = noDataStr;
    [self setTitle:noDataStr forState:MJRefreshStateNoMoreData];
    self.footerIcon.hidden = noDataStr.length > 0;
}

- (UIImageView *)footerIcon {
    if (!_footerIcon) {
        _footerIcon = [UIImageView new];
        _footerIcon.image = [[DLThemeAdapter shareInstance] dynamicImage: [UIImage imageNamed:kDLUIKitImageName(@"icon_footer")] darkImage: [UIImage imageNamed:kDLUIKitImageName(@"icon_footer_dark")]];
        [self addSubview:self.footerIcon];
        [self.footerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
            make.width.mas_equalTo(141);
            make.height.mas_equalTo(6);
        }];
    }
    return _footerIcon;
}

- (void)show{
    _refreshAnimationView = [[DLRefreshAnimationView alloc] init];
    _refreshAnimationView.refreshText = _refreshText;
    _refreshAnimationView.lab.hidden = !_isShowTextAndCircleView;
    _refreshAnimationView.refreshTextShowBottom = _refreshTextShowBottom;
    _refreshAnimationView.backgroundColor = [UIColor clearColor];
    _refreshAnimationView.layer.cornerRadius = 17.5;
    _refreshAnimationView.layer.shadowColor = [UIColor blackColor].CGColor;
    _refreshAnimationView.layer.shadowOpacity = 0.1;
    _refreshAnimationView.layer.shadowOffset = CGSizeMake(3, 3);
    _refreshAnimationView.layer.shadowRadius = 5.0;
    _refreshAnimationView.tintColor = self.tintColor;
    [_refreshAnimationView buildUI];
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:DLRefreshAnimationView.class]) {
            [subView removeFromSuperview];
        }
    }
    [self addSubview:_refreshAnimationView];
}

- (void)setRefreshTextShowBottom:(BOOL)refreshTextShowBottom{
    _refreshTextShowBottom = refreshTextShowBottom;
}

- (void)setIsShowTextAndCircleView:(BOOL)isShowTextAndCircleView{
    _isShowTextAndCircleView = isShowTextAndCircleView;
}

- (void)setRefreshText:(NSString *)refreshText{
    _refreshText = refreshText;
    _refreshAnimationView.refreshText = _refreshText;
}
@end
